package cate.game.play.skill.passive.awaken;

import cate.common.table.d.GDSkill;
import cate.common.util.XT;
import cate.game.attr.FightAttr;
import cate.game.play.base.TargetFilterParam;
import cate.game.play.base.TargetSelector;
import cate.game.play.buff.Buff;
import cate.game.play.buff.BuffParam;
import cate.game.play.config.PlayCfgArgs;
import cate.game.play.fighter.Fighter;
import cate.game.play.proce.action.ctx.ActionCtx;
import cate.game.play.proce.action.ctx.SkillActionCtx;
import cate.game.play.skill.passive.PassiveHandler;
import cate.game.play.skill.passive.hero.英雄觉醒PH;

import java.util.List;

/**
 * 效果1：进入觉醒状态发动技能【制衡·愈合】,随机从攻击、伤害减免、伤害加深中选择1种作为制衡属性,
 *       然后选择1名制衡属性最高的敌方英雄,降低其25%制衡基础属性并转移至自身,持续4回合;
 *       觉醒状态下回合开始时,为1名友方单位附加【协衡·愈合】1回合,提升目标的伤害和治疗加成,数值为【觉醒·愈合】的40%
 * 效果2：【觉醒·愈合】额外提升8%仙术攻击和8%仙术防御;
 *              觉醒状态下攻击觉醒值比自己高的目标时有50%概率降低其25%觉醒值,
 *              辅助觉醒值比自己低的目标时有50%概率提升其25%觉醒值
 * 效果3：【制衡·愈合】属性比例提高至35%,状态结束后提升自身10%制衡的属性,持续到战斗结束;
 *              被制衡的目标下回合觉醒值无法增长且无法进入觉醒状态,若处于觉醒状态则失效1回合
 */
public class 世界树的嫩枝PH extends PassiveHandler {


	private List<Integer> 制衡属性;
	private List<Integer> 制衡buff;
	private int 愈合buff;
	private double 愈合系数;
	private int 降低概率;
	private int 辅助概率;
	private double 觉醒变更;
	private int 觉醒失效buff;

	//制衡属性=2#24#23&制衡buff=??#??#?？&
	//愈合buff=？？&愈合系数=4000&
	//降低概率=5000&辅助概率=5000&觉醒变更=2500&
	//觉醒失效buff=？？
	@Override
	public void parse(String code) {
		PlayCfgArgs args = new PlayCfgArgs(code);
		制衡属性 = args.getIntList("制衡属性");
		制衡buff = args.getIntList("制衡buff");
		愈合buff = args.getInt("愈合buff", 0);
		愈合系数 = args.getWDouble("愈合系数", 0d);
		降低概率 = args.getInt("降低概率", 0);
		辅助概率 = args.getInt("辅助概率", 0);
		觉醒变更 = args.getDouble("觉醒变更", 0d);
		觉醒失效buff = args.getInt("觉醒失效buff", 0);
	}

	@Override
	public void onFirstEnter(ActionCtx action) {
		英雄觉醒ph = skill.owner.skill.getHandler(英雄觉醒PH.class);
	}

	@Override
	public void onAwaken(ActionCtx action, boolean start) {
		if(start){
			handleSteal(action);
			handleAddBuff(action);
		}
	}

	@Override
	public void beforeRoundBegin(ActionCtx action) {
		if (stealTarget != null) {
			if (觉醒失效buff > 0) {
				stealTarget.buff.tryAddByTid(action, skill.owner, 觉醒失效buff);
			}
			stealTarget = null;
		}
	}

	private Fighter stealTarget;

	private void handleSteal(ActionCtx action) {
		if (XT.isBlank(制衡属性) || XT.isBlank(制衡buff)) {
			return;
		}
		if (制衡属性.size() != 制衡buff.size()) {
			return;
		}
		int index = XT.randomIndex(制衡属性.size());
		int attr = 制衡属性.get(index);
		TargetFilterParam param = new TargetFilterParam().bigAttrSet(attr);
		Fighter one = TargetSelector.findOne(skill.owner, action.getTeamVs(), param);
		if (one != null) {
			int buff = 制衡buff.get(index);
			one.buff.tryAddByTid(action, skill.owner, buff);
			stealTarget = one;
		}
	}

	private 英雄觉醒PH 英雄觉醒ph;

	private void handleAddBuff(ActionCtx action) {
		if (愈合buff <= 0 || 英雄觉醒ph == null) {
			return;
		}
		int awakenBuff = 英雄觉醒ph.getAwakenBuff();
		if (awakenBuff <= 0) {
			return;
		}
		Buff oneByTid = skill.owner.buff.findOneByTid(awakenBuff);
		if (oneByTid == null) {
			return;
		}
		FightAttr exAttr = oneByTid.cfg.getExAttr().scale(愈合系数);
		TargetFilterParam param = new TargetFilterParam().scopeSet(GDSkill.Scope.FRIENDS);
		Fighter one = TargetSelector.findOne(skill.owner, action.getTeamVs(), param);
		if (one != null) {
			one.buff.tryAddByTid(action, skill.owner, 愈合buff, new BuffParam().attrSet(exAttr));
		}
	}

	@Override
	public void afterOneAttack(SkillActionCtx action, long totalDamage, boolean strike, Fighter target) {
		if (!skill.owner.status.isAwaken()) {
			return;
		}
		if (skill.owner.skill.getEnergy() >= target.skill.getEnergy()) {
			return;
		}
		if (!XT.chance(降低概率)) {
			return;
		}
		target.skill.addAwakenEnergy(action, -觉醒变更);
	}


	@Override
	public void onSkillDone(SkillActionCtx action) {
		if (!skill.owner.status.isAwaken()) {
			return;
		}
		if (!action.getSkill().cfg.isSupportSkill()) {
			return;
		}

		for (Fighter target : action.getTargets()) {
			if (skill.owner.skill.getEnergy() <= target.skill.getEnergy()) {
				continue;
			}
			if (!XT.chance(辅助概率)) {
				continue;
			}
			target.skill.addAwakenEnergy(action, 觉醒变更);
		}
	}
}
